/**
 * 
 */
package edu.tsinghua.mrpvc.device.curtain;

import java.util.Calendar;
import java.util.Hashtable;
import java.util.Map;

import org.osgi.framework.BundleContext;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventAdmin;
import org.osgi.service.event.EventHandler;
import org.osgi.util.tracker.ServiceTracker;

import edu.tsinghua.mrpvc.device.CurtainDevice;


/**
 * @author Tangjian Deng & Wei Feng
 *
 */
public class CurtainDeviceImpl implements CurtainDevice,EventHandler {
	String ID;
	int posState;
	boolean valid;
	
	ServiceTracker tracker;
	
	public CurtainDeviceImpl(BundleContext context, String ID){
		valid = true;
		posState = 1;
		this.ID = ID;
		tracker = new ServiceTracker(context, EventAdmin.class.getName(), null);
		tracker.open();		
	}

	void generEvent(){
		EventAdmin eventAdmin = (EventAdmin) tracker.getService();
		if(eventAdmin != null){
			System.out.println(Calendar.getInstance().getTime().toLocaleString() +
					" Curtain: state changed and post event");
			Map<String, String> propers = new Hashtable<String, String>();
			propers.put(CurtainDevice.EVENT_PROPER_ID, ID);
			propers.put(CurtainDevice.EVENT_PROPER_STATE, posState == 1 ? CurtainDevice.Constants.STATE_OPEN : CurtainDevice.Constants.STATE_CLOSE);
			propers.put(CurtainDevice.EVENT_PROPER_ISVALID, valid == true ? CurtainDevice.Constants.ISVALID_TRUE : CurtainDevice.Constants.ISVALID_FALSE);
			Event event = new Event(CurtainDevice.EVENT_TOPIC_STATE, propers);
			eventAdmin.postEvent(event);
		}
	}
	@Override
	public int close() {
		// TODO Auto-generated method stub
		posState = 0;
		System.out.print(Calendar.getInstance().getTime().toLocaleString() + " ");
		System.out.println("Curtain: close executed");
		generEvent();
		return 1;
	}

	@Override
	public String getID() {
		// TODO Auto-generated method stub
		return ID;
	}

	@Override
	public int getState() {
		// TODO Auto-generated method stub
		return posState;
	}

	@Override
	public boolean isValid() {
		// TODO Auto-generated method stub
		return valid;
	}

	@Override
	public int open() {
		// TODO Auto-generated method stub
		posState = 1;
		System.out.print(Calendar.getInstance().getTime().toLocaleString() + " ");
		System.out.println("Curtain: open executed");
		generEvent();
		return 1;
	}

	@Override
	public void handleEvent(Event event) {
		// TODO Auto-generated method stub
		if(event.getTopic().equals(CurtainDevice.EVENT_TOPIC_ISVALID)){
			if(event.getProperty(CurtainDevice.EVENT_PROPER_ID) != null && event.getProperty(CurtainDevice.EVENT_PROPER_ID).equals(ID)){
				generEvent();
			}
			else{
				System.out.println("***Curtain ID not match***");
			}
		}
		else if(event.getTopic().equals(CurtainDevice.EVENT_TOPIC_CURSTATE)){
			if(event.getProperty(CurtainDevice.EVENT_PROPER_ID) != null && event.getProperty(CurtainDevice.EVENT_PROPER_ID).equals(ID)){
				generEvent();
			}
			else{
				System.out.println("***Curtain ID not match***");
			}
		}
		else if(event.getTopic().equals(CurtainDevice.EVENT_TOPIC_ACTION)){
			if(event.getProperty(CurtainDevice.EVENT_PROPER_ID) != null && event.getProperty(CurtainDevice.EVENT_PROPER_ID).equals(ID)){
				if(event.getProperty(CurtainDevice.EVENT_PROPER_ACTION) != null){
					if(event.getProperty(CurtainDevice.EVENT_PROPER_ACTION).equals(CurtainDevice.Constants.ACTION_CLOSE)){
						this.posState = 0;
					}
					else if(event.getProperty(CurtainDevice.EVENT_PROPER_ACTION).equals(CurtainDevice.Constants.ACTION_OPEN)){
						this.posState = 1;
					}
				}
				generEvent();
			}
			else{
				System.out.println("***Curtain ID not match***");
			}
			
		}		
	}

}
